/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.controller;

import com.fandi.model.CategoryModel;
import com.fandi.model.ItemModel;
import com.fandi.model.ItemTableModel;
import com.fandi.util.Message;
import com.fandi.view.ItemFrame;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fandy
 */
public class ItemController {
    private ItemModel model;
    private CategoryModel categoryModel;
    private ItemTableModel tableModel;

    // For update data purpose
    private boolean isUpdate;
    private int index;
    private String code;
    
    public void setModel(ItemModel model) {
        this.model = model;
    }

    public void setCategoryModel(CategoryModel categoryModel) {
        this.categoryModel = categoryModel;
    }
    
    public void setTableModel(ItemTableModel tableModel) {
        this.tableModel = tableModel;
    }
    
    public void insert(ItemFrame view) {
        String code             = view.getTxtItemCode().getText();
        String name             = view.getTxtItemName().getText();
        CategoryModel category  = (CategoryModel) view.getCmbItemCat().getSelectedItem();
        Integer qty             = (Integer) view.getTxtItemQty().getValue();
        Date expiredDate        = view.getTxtItemExpired().getDate();
        //String fmtExpiredDate   = DateFormatterUtil.formatNow("", expiredDate);
        String desc             = view.getTxtItemDesc().getText();
        
        try {
            if (code.equals("")) {
                Message.showMessage("Code is required", "Error", Message.ERROR);
            } else if (name.equals("")) {
                Message.showMessage("Name is required", "Error", Message.ERROR);
            } else {
                model.setCode(code);
                model.setName(name);
                model.setCatId(category.getId());
                model.setQty(qty);
                model.setExpired(expiredDate);
                model.setDescription(desc);

                
                if(isUpdate) {
                    model.update(code);
                } else {
                    model.insert();
                }
                
                Message.showMessage("Successfully saved!", "Information", Message.INFO);
                
                renderTableData(view, false, null);
                model.reset();
                
                view.getBtnRemove().setEnabled(false);
                view.getTblItemData().clearSelection();
                
                setTotalItems(view);
                setExpiredItems(view);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }
    
    public void reset(ItemFrame view) {
        model.reset();
        
        isUpdate = false;
        view.getBtnRemove().setEnabled(false);
        view.getTblItemData().clearSelection();
    }

    public void renderTableData(ItemFrame view, boolean isSearch, String strCode) {
        List<ItemModel> list = null;
        try {
            if (!isSearch) {
                list = model.selectAll();
            } else {
                list = model.searchBy(strCode);
            }
            
            tableModel.setList(list);
            view.getTblItemData().setModel(tableModel);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void selectItem(ItemFrame view) {
        try {
            index   = view.getTblItemData().getSelectedRow();
            code    = (String) view.getTblItemData().getValueAt(index, 0);
            ItemModel data = model.selectById(code);
            
            model.setUid(data.getUid());
            model.setCode(data.getCode());
            model.setName(data.getName());
            model.setCatId(data.getCatId());
            model.setQty(data.getQty());
            model.setExpired(data.getExpired());
            model.setDescription(data.getDescription());
            
            isUpdate = true;
            view.getBtnRemove().setEnabled(true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public void remove(ItemFrame view) {
        try {
            int index   = view.getTblItemData().getSelectedRow();
            String code = (String) view.getTblItemData().getValueAt(index, 0);
            
            if (model.delete(code)) {
                Message.showMessage("Successfully deleted!", "Success", Message.INFO);
                renderTableData(view, false, null);
            }
            
            reset(view);
            
            setTotalItems(view);
            setExpiredItems(view);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void populateCategory(ItemFrame view) {
        
        view.getCmbItemCat().removeAllItems();
        
        try {
            List<CategoryModel> list = categoryModel.selectAll();
            
            for (CategoryModel category : list) {
                view.getCmbItemCat().addItem(category);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void search(ItemFrame view) {
        String code = view.getTxtSearch().getText();
        renderTableData(view, true, code);
    }

    public String getTotalItems() {
        try {
            String count = String.valueOf(model.count());
            
            return count;
        } catch (SQLException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public String getExpiredItems() {
        try {
            String count = String.valueOf(model.countExpired());
            
            return count;
        } catch (SQLException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public void setTotalItems(ItemFrame view) {
        view.getLblTotalItems().setText(getTotalItems());
    }
    
    public void setExpiredItems(ItemFrame view) {
        view.getLblExpiredItems().setText(getExpiredItems());
    }
}
